<template>
	<view class="box">
		<view class="top">
			<view class="position">
				<view class="image" @click="getuserinfo">
					<image src="../../static/weizhi.png" mode=""></image>
				</view>
				<view class="address">
					{{address.address}}
				</view>
			</view>
			<view class="cu-bar search bg-white" style="width: 100%;" @click="tostorelist">
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<input @focus="InputFocus" @blur="InputBlur" disabled="true" :adjust-position="false" type="text" placeholder="Search"
					 confirm-type="search"></input>
				</view>
				<view class="action">
					<!-- <button class="cu-btn bg-green shadow-blur round">搜索</button> -->
				</view>
			</view>

		</view>
		<view class="swiper">
			<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
				<swiper-item v-for="(item,index) in swiperlist" :key="index">
					<image :src="imgurl + item.pic" mode=""></image>
				</swiper-item>
			</swiper>
		</view>
		<view class="content">
			<view class="sort">
				<picker :range="array" :value="index" @change="bindPickSort">
					<view class="select">
						<view class="">
							{{array[index]}}
						</view>
						<view class="image">
							<image src="../../static/xiajiantou.png" mode=""></image>
						</view>
					</view>
				</picker>
			</view>
			<view class="list" v-for="(item,index) in shopplist" :key="index" @click="gotoStore(item)">
				<view class="list_item">
					<view class="img">
						<image :src="imgurl + item.image" mode=""></image>
					</view>
					<view class="list_content">
						<text class="shopName">{{item.name}}</text>
						<text class="Grade">{{item.level}}</text>
						<text class="address">{{item.detailAddress}}</text>
					</view>
					<view class="distance">
						{{item.distance}}km
					</view>
				</view>
			</view>
		</view>
		<view class="tabbar">
		</view>
		<tabBar :pagePath="'/pagesA/index/index'" :type='type'></tabBar>


	</view>
</template>

<script>
	var app = getApp()
	export default {
		data() {
			return {
				array: ['距离', '等级', '常用'],
				index: 0,
				type: 1,
				InputBottom: 0,
				swiperlist: [],
				address: '',
				page: 1, //推荐列表页码
				imgurl: '',
				shopplist: [], //推荐列表
			}
		},
		onLoad() {
			var that = this
			that.imgurl = that.$imgurl
			uni.getLocation({
				type: 'wgs84',
				success: function(res) {
					console.log('当前位置的经度：' + res.longitude);
					console.log('当前位置的纬度：' + res.address);

					var data = {
						longitude: res.longitude,
						latitude: res.latitude,
					}

					that.$http('/api/gaodeMap/getAddressByMap', 'POST', data).then((res) => {

						data.address = res.data.data
						that.address = data
						that.getshoplist(); //获取店铺列表
						console.log('地理位置信息', that.address)
					})

				}
			});
			that.getswiper(); //获取首页轮播图
		},
		onReachBottom() {
			this.bottomingout()
			console.log('到底了', this.page)
		},
		methods: {
			
			//触底刷新
			bottomingout() {
				if (this.shopplist.length % 5 === 0) {
					this.page++
					this.getshoplist()
				} else {
					uni.showToast({
						title: "已经到底了！",
						icon: "none"
					})
					return false
				}
			},
			//门店详情
			gotoStore(item) {
				var data = {
					latitude: this.address.latitude,
					longitude: this.address.longitude,
					storeId: item.id
				}
				app.globalData.shopid = item.id
				uni.navigateTo({
					url: "../stroeDetails/stroeDetails?data=" + JSON.stringify(data)
				})
			},
			//店铺列表
			tostorelist() {
				uni.navigateTo({
					url: "../storeList/storeList"
				})
			},
			//获取店铺列表
			getshoplist() {
				var that = this
				console.log('请求数据', that.address)
				var data = {
					keywords: '',
					latitude: that.address.latitude,
					longitude: that.address.longitude,
					order: that.index
				}
				if (that.index == 2) {
					data.userId = 1
				}
				var http = '/api/user/index/storeList?' + 'limit=' + 5 + '&page=' + that.page
				console.log('请求方式', http)
				that.$http(http, 'POST', data).then((res) => {
					if (res.data.data.list.length <= 0) {
						--that.page
						console.log("已经到底了")
						uni.showToast({
							title: "已经到底了！",
							icon: "none"
						})
						return false
					}
					that.shopplist = that.shopplist.concat(res.data.data.list)
					console.log('返回商品列表', that.shopplist)
				})
			},
			//获取轮播图
			getswiper() {
				var that = this
				var data = {}
				that.$http('/api/user/index/bannerInfo', 'GET', data).then((res) => {
					console.log('返回参数', res)
					that.swiperlist = res.data.data.banner
				})
			},
			saoyisao() {
				uni.scanCode({
					success: function(res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
					}
				});
			},
			bindPickSort: function(e) {
				console.log('picker发送选择改变，携带值为', e.target.value)
				this.index = e.target.value
				this.page = 1
				this.shopplist = []
				this.getshoplist()
			},
			InputFocus(e) {
				this.InputBottom = e.detail.height
			},
			InputBlur(e) {
				this.InputBottom = 0
			}
		}
	}
</script>

<style lang="scss">
	// page{
	// 	height: 100vh;
	// }
	.swiper {
		width: 100%;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.box {
		width: 100%;
		height: 100%;

		.top {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 0 20rpx;

			.position {
				flex: 0 0 30%;
				display: flex;
				align-items: center;

				.image {
					width: 40rpx;
					height: 40rpx;
					margin-right: 10rpx;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.address {}
			}
		}

		.content {
			width: 750rpx;

			.sort {
				padding: 20rpx;
				margin: 10rpx 0;
				border: 1rpx solid #ECECEC;

				.select {
					display: flex;
					align-items: center;

					.image {
						width: 30rpx;
						height: 30rpx;
						margin-left: 10rpx;

						image {
							width: 100%;
							height: 100%;
						}
					}
				}

			}

			.list {
				width: 750rpx;

				.list_item {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-top: 10rpx;
					padding: 20rpx 40rpx;
					font-size: 25rpx;
					box-sizing: border-box;
					border-bottom: 1rpx solid #ECECEC;

					.list_content {
						flex: 0 0 60%;
						display: flex;
						flex-direction: column;
						justify-content: center;
						line-height: 50rpx;

						.shopName {
							font-size: 32rpx;
							color: rgba(0, 0, 0, 0.76);
							font-weight: 550;
						}

						.Grade {
							width: 104rpx;
							height: 40rpx;
							background-color: #f49c1c;
							color: #FFFFFF;
							border-radius: 50rpx;
							font-size: 20rpx;
							display: flex;
							align-items: center;
							justify-content: center;
						}

						.address {
							width: 400rpx;
							color: #7f7f7f;
							font-size: 26rpx;
							overflow: hidden;
							/*超出部分隐藏*/
							white-space: nowrap;
							/*不换行*/
							text-overflow: ellipsis;
							/*超出部分文字以...显示*/
						}
					}

					.distance {
						color: #a3a3a3;
						font-size: 24rpx;
					}

					.img {
						width: 130rpx;
						height: 130rpx;

						image {
							width: 100%;
							height: 100%;
							border-radius: 10rpx;
						}
					}
				}

			}
		}

		.tabbar {
			width: 750rpx;
			height: 100rpx;
		}
	}
</style>
